package com.eatables.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.eatables.util.DBUtil;
import com.eatables.util.QueryConstants;

public class EatablesMasterDao {

	@SuppressWarnings("unchecked")
	public List getAllHotelIDsDetails() {
		List result = null;
		try {
			// TODO pass the query from here instead of setting in DBUtil
			result = DBUtil.buildListOfMapRecords(DBUtil
					.executeMyQuery(QueryConstants.GET_ALL_DETAILS));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("Exception" + e);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List getAllHotelIDsDetailsForChennai() {
		List result = null;
		try {
			// TODO pass the query from here instead of setting in DBUtil
			result = DBUtil.buildListOfMapRecords(DBUtil
					.executeMyQuery(QueryConstants.GET_ALL_DETAILS_CHN));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("Exception" + e);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List getAllHotelIDsDetailsForKolkata() {
		List result = null;
		try {
			// TODO pass the query from here instead of setting in DBUtil
			result = DBUtil.buildListOfMapRecords(DBUtil
					.executeMyQuery(QueryConstants.GET_ALL_DETAILS_KOL));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("Exception" + e);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List getAllHotelIDsDetailsForBengaluru() {
		List result = null;
		try {
			// TODO pass the query from here instead of setting in DBUtil
			result = DBUtil.buildListOfMapRecords(DBUtil
					.executeMyQuery(QueryConstants.GET_ALL_DETAILS_BEN));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("Exception" + e);
		}
		return result;
	}

	public int setHotelData(String rname, String type, String lng, String lat,String address, String phone, String descr, String musthav, String name, String mailid) {
		//masterDao.setHotelData(rname, type, lng, lat, address, phone, descr, musthav,name, mailid);
		System.out.println("entering into setHotelData");
		// TODO send the data to DB and get the return type
		// insert into ucontribute
		// values(2,9.9,9.9,'rname','lmark','999','mhav','desc','uname','uloc');
		String insQuery = "insert into ucontribute(lat,lng,rname,landmark,phoneno,musthaves,descrip,urname,uremail, typeof, addr) values("
				+ lat
				+ ","
				+ lng
				+ ",'"
				+ rname
				+ "','"
				+ address
				+ "','"
				+ phone
				+ "','"
				+ musthav
				+ "','"
				+ descr
				+ "','"
				+ name
				+ "','"
				+ mailid
				+ "','"
				+ type
				+ "','"
				+ address
				+ "')";
		System.out.println("Insert Query  "+insQuery);
		int status = DBUtil.insertQuery(insQuery);
		return status;
	}

	public List searchHotelsData(String radius, String lng, String lat) {
		System.out.println("Entering into SearchHotelData");
		// TODO send the data to DB and get the return type
		// insert into ucontribute
		// values(2,9.9,9.9,'rname','lmark','999','mhav','desc','uname','uloc');
		List result = null;
		String insQuery = "SELECT replace(replace(descr,'&reg;',' '),'&',' ') 'descr', "
				+ "replace(replace(rname,'&Eacute;',' '),'&',' ') 'rname',"
				+ "typeof, "
				+ "lat, "
				+ "lng, "
				+ "truncate( ( 6371 * acos( cos( radians('"
				+ lat
				+ "') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('"
				+ lng
				+ "') ) + sin( radians('"
				+ lat
				+ "') ) * sin( radians( lat ) ) ) ) ,2)AS distance "
				+ "FROM tbl_master_list HAVING distance < '"
				+ radius
				+ "' "
				+ "ORDER BY distance ";
		System.out.println("Query " + insQuery);

		try {
			result = DBUtil.buildListOfMapRecords(DBUtil
					.executeMyQuery(insQuery));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("Exception" + e);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List getHotelData(String slno, String city) {
		System.out.println("entering into getHotelData" + slno + "------"
				+ city);
		// TODO send the data to DB and get the return type
		// insert into ucontribute
		// values(2,9.9,9.9,'rname','lmark','999','mhav','desc','uname','uloc');
		ResultSet rs = null;
		if (city == "chn" || city.equals("chn")) {
			rs = DBUtil.executeMyQuery(QueryConstants.GET_HOTEL_DETAILS_CHN
					+ "'" + slno + "'");
			System.out.println("Query:" + QueryConstants.GET_HOTEL_DETAILS_CHN
					+ " " + slno + " ");
		} else if (city == "kol" || city.equals("kol")) {
			rs = DBUtil.executeMyQuery(QueryConstants.GET_HOTEL_DETAILS_KOL
					+ "'" + slno + "'");
			System.out.println("Query:" + QueryConstants.GET_HOTEL_DETAILS_KOL
					+ " " + slno + " ");
		} else if (city == "ben" || city.equals("ben")) {
			rs = DBUtil.executeMyQuery(QueryConstants.GET_HOTEL_DETAILS_BEN
					+ "'" + slno + "'");
			System.out.println("Query:" + QueryConstants.GET_HOTEL_DETAILS_BEN
					+ " " + slno + " ");
		} else if (city == "hyd" || city.equals("hyd")){
			System.out.println("City is HYD");
			rs = DBUtil.executeMyQuery(QueryConstants.GET_HOTEL_DETAILS + "'"
					+ slno + "'");
			System.out.println("Query:" + QueryConstants.GET_HOTEL_DETAILS
					+ " " + slno + " ");
		}
		ArrayList data = new ArrayList();
		int index = 1;
		try {
			while (rs.next()) {
				data.add(rs.getString(1));
				data.add(rs.getString(2));
				data.add(rs.getString(3));
				data.add(rs.getString(4));
				data.add(rs.getString(5));
				data.add(rs.getString(6));// lat
				data.add(rs.getString(7));// lng
				data.add(rs.getString(8));
				data.add(rs.getString(9));
				data.add(rs.getString(10));
				System.out.println("index");
				index++;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (int i = 0; i < data.size(); i++)
			System.out.println("-----DAO------ " + data.get(i));

		return data;
	}
}
